package cn.edu.nchu.fragmentdemo.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.edu.nchu.fragmentdemo.R;

/**
 * RecyclerView adapter基类
 * <p>
 * 封装了数据集合以及ItemView的点击事件回调
 * 用于数据与view绑定
 *
 * @param <T> A data bean class that will be used by the adapter.
 *            <p>
 */

abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    /**
     * 数据源
     */
    private List<T> mData;

    private int mLayoutId;

    /**
     * 是否有更多数据
     */
    private boolean hasMore = false;

    /**
     * 是否隐藏底部 tips
     */
    private boolean fadeTips = true;

    /**
     * 普通viewitem类型
     */
    private static final int VIEW_TYPE_ITEM = 0;
    /**
     * 底部viewitem 用于提示 正在加载数据/没有更多数据
     */
    private static final int VIEW_TYPE_FOOTER = 1;
    /**
     * item点击监听器
     */
    private OnItemClickListener mListener;

    /**
     * 每页容纳的item数量
     */
    private int count = 10;


    BaseRecyclerViewAdapter(Context context, List<T> data, int layoutId , boolean hasMore) {
        this.mContext = context;
        this.mData = data;
        this.mLayoutId = layoutId;
        this.hasMore = hasMore;
    }

    /**
     * 根据条目位置返回ViewType，以供onCreateViewHolder方法内获取不同的Holder
     * 如果是最后一项 （postion从0开始）则返回类型为底部View
     */
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return VIEW_TYPE_FOOTER;
        } else {
            return VIEW_TYPE_ITEM;
        }
    }


    public List<T> getmData() {
        return mData;
    }

    public boolean isFadeTips() {
        return fadeTips;
    }

    public void setFadeTips(boolean fadeTips) {
        this.fadeTips = fadeTips;
    }

    /**
     * 传入是否有更多数据
     *
     * @param hasMore
     */
    public void updateList(boolean hasMore) {
        this.hasMore = hasMore;
        notifyDataSetChanged();
    }

    public void resetDatas() {
        mData.clear();
    }

    /**
     * 获取ViewItem的个数 包括底部View
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    /**
     * 自定义方法，获取列表中数据源的最后一个位置，比getItemCount少1，因为不计上footView
     *
     * @return
     */
    public int getRealLastPosition() {
        return mData.size();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_FOOTER) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_footer, parent, false);
            return new FootViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(mLayoutId, parent, false);
            view.setOnClickListener(this);
            return new ItemViewHolder(view);
        }
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //如果类型为普通item 则绑定数据
        if (holder instanceof ItemViewHolder) {
            holder.itemView.setTag(position);
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            T bean = mData.get(position);
            onBindData(itemViewHolder, bean, position);
        } else {
            final FootViewHolder footViewHolder = (FootViewHolder) holder;
            if (mData.size() == 0){
                footViewHolder.getTips().setVisibility(View.GONE);
                footViewHolder.getProgressBar().setVisibility(View.GONE);
            }
            //不隐藏底部提示的情况下
            if (!isFadeTips()){
                //如果有更多的数据 则显示数据加载中 如果请求的数据为空则显示没有更多数据
                if (hasMore) {
                    if (mData.size() > 0) {
                        footViewHolder.getProgressBar().setVisibility(View.VISIBLE);
                        footViewHolder.getTips().setVisibility(View.VISIBLE);
                        footViewHolder.getTips().setText("数据加载中...");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                // 设置提示文本和视图可见性
                                footViewHolder.getTips().setVisibility(View.GONE);
                                footViewHolder.getProgressBar().setVisibility(View.GONE);
                            }
                        }, 2500);
                    }
                } else {
                    if (mData.size() > 0) {
                        footViewHolder.getProgressBar().setVisibility(View.VISIBLE);
                        footViewHolder.getTips().setVisibility(View.VISIBLE);
                        footViewHolder.getTips().setText("没有更多数据了");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                // 设置提示文本和视图可见性
                                footViewHolder.getTips().setVisibility(View.GONE);
                                footViewHolder.getProgressBar().setVisibility(View.GONE);
                            }
                        }, 2000);
                        hasMore = true;
                    }
                }
            }
        }

    }


    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onItemClick(this, v, (Integer) v.getTag());
        }
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mListener = onItemClickListener;
    }

    /**
     * 数据绑定，由实现类实现
     *
     * @param holder   The reference of the all view within the item.
     * @param bean     The data bean related to the position.
     * @param position The position to bind data.
     */
    protected abstract void onBindData(ItemViewHolder holder, T bean, int position);


    /**
     * item点击监听器
     */
    public interface OnItemClickListener {
        /**
         * item点击回调
         *
         * @param adapter  The Adapter where the click happened.
         * @param v        The view that was clicked.
         * @param position The position of the view in the adapter.
         */
        void onItemClick(RecyclerView.Adapter adapter, View v, int position);
    }


}
